<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :form_data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div class="c_666 fw">项目结构设置</div>
        <div class="flex">
          <el-button type="primary" @click="show_box = true">新增</el-button>

          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        highlight-current-row
        v-loading="loading"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :row-style="{
          height:
            height_size == 1 ? '58px' : height_size == 2 ? '48px' : '38px',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="项目结构名称"
          prop="name"
          key="项目结构名称"
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="项目结构代号"
          prop="account"
          key="项目结构代号"
          v-if="header_list[1].showTable"
        ></el-table-column>
        <el-table-column
          label="状态"
          prop="phone"
          key="状态"
          v-if="header_list[2].showTable"
        ></el-table-column>
        <el-table-column
          label="结构描述"
          prop="weixin"
          key="结构描述"
          v-if="header_list[3].showTable"
        ></el-table-column>
        <el-table-column label="操作" width="300px">
          <template #default="scope: any">
            <el-button>启用</el-button>
            <el-button>编辑</el-button>
            <el-button>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
    </div>
  </div>
  <el-drawer
    v-model="show_box"
    direction="rtl"
    @close="resetForm(addcontentsetform)"
    :close-on-click-modal="false"
  >
    <template #title>
      <h4 v-if="add_form_data.id == ''">新增项目结构设置</h4>
      <h4 v-else>编辑项目结构设置</h4>
    </template>
    <template #default>
      <el-form
        :model="add_form_data"
        ref="addcontentsetform"
        label-width="120px"
        :rules="rules"
      >
        <el-form-item label="行业分类" prop="type">
          <el-select
            v-model="add_form_data.type"
            multiple
            style="width: 100%; height: 32px"
          >
            <el-option
              v-for="item in dict_list"
              :key="item.id"
              :label="item.dictValue"
              :value="item.dictCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结构名称" prop="name">
          <el-input
            v-model="add_form_data.name"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="结构代号" prop="code">
          <el-input
            v-model="add_form_data.code"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-model="add_form_data.status" :label="1">已启用</el-radio>
          <el-radio v-model="add_form_data.status" :label="0">已停用</el-radio>
        </el-form-item>
        <el-form-item label="结构描述">
          <el-input
            type="textarea"
            v-model="add_form_data.remark"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <div class="tr m_top10">
          <el-button @click="resetForm(addcontentsetform)">取消</el-button>
          <el-button type="primary" @click="submit(addcontentsetform)"
            >确定</el-button
          >
        </div>
      </el-form>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { check_role, dictData, debounce } from '@/utils/check_role' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, reactive, onUnmounted, nextTick } from 'vue'
import EventBus from '@/utils/EventBus'
import { FormInstance, FormRules, ElMessage } from 'element-plus'
// type FormInstance = InstanceType<typeof ElForm>
const addcontentsetform = ref<FormInstance>()
const rules = reactive<FormRules>({
  name: [{ required: true, message: '请输入结构名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入结构代号', trigger: 'blur' }],
  type: [
    {
      required: true,
      type: 'array',
      message: '请选择行业类型',
      trigger: 'change',
    },
  ],
})
const list = ref()
const total = ref()
const max_height = ref()
nextTick(() => {
  max_height.value = window.innerHeight - 320 // 不带分页的 table 高度
})
const loading = ref(false)
const show_box = ref(false)
const height_size = ref(1)
const header_list = ref([
  {
    label: '项目结构名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '项目结构代号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '状态',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '结构描述',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
])
const page_data = ref({
  current: 1,
  pageSize: 10,
  name: '',
  phone: '',
})
const add_form_data = ref({
  id: '',
  name: '',
  code: '',
  type: [],
  remark: '',
  status: 1,
})

const dict_list = ref()
onMounted(() => {
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })
  get_list()
  dictData('1541665967619182594').then((res) => {
    console.log(res)
    dict_list.value = res as any
  }) // 获取字典值
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const submit = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    console.log(add_form_data.value)
    if (valid) {
      //  TODO
    }
  })
}
const get_list = () => {}
let list_debounce = debounce(200, get_list)
const search_key = () => {}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  add_form_data.value = {
    id: '',
    name: '',
    code: '',
    remark: '',
    status: 1,
    type: [],
  }
  show_box.value = false
}
</script>

<style lang="scss" scoped>
:deep(.el-input__inner) {
  height: 32px !important;
}
</style>
